﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MarketOnlineBUS;
using MarketOnlineDTO;
using System.Data;


public partial class cms_ManageBill : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDanhSachHD();
        }
    }
    void LoadDanhSachHD()
    {
        List<HoaDonDTO> list = new List<HoaDonDTO>();

        list = new HoaDonBUS().LayDanhSachHoaDon();

        
        GridView1.DataSource = list;
        GridView1.DataBind();
        //Dispose();
    }
    //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    GridView1.PageIndex = e.NewPageIndex;
    //    LoadDanhSachHD();
    //}
    //protected void GridView1_RowCommand(object sender,System.Web.UI.WebControls.GridViewCommandEventArgs e)
    //{

    //    string currentCommand = e.CommandName;
    //    //int currentRowIndex = Int32.Parse(e.CommandArgument);
    //    //string ProductID = GridView1.DataKeys[currentRowIndex].Value;

    //    //Label1.Text = "Command: " + currentCommand;
    //    //Label2.Text = "Row Index: " + currentRowIndex.ToString;
    //    //Label3.Text = "Product ID: " + ProductID;
    //}

    //protected void btnCapNhat_Click(object sender, EventArgs e)
    //{
        //List<HoaDonDTO> list = new List<HoaDonDTO>();
        //list = (List<HoaDonDTO>)Session["cms_ManageBill"];

        //int i = 1;
        //int j = 1;
        //HoaDonDTO dto = new HoaDonDTO();
        //int MaHD;
        //string TenKH;
        //string DienThoai;
        //string Email;
        //DateTime NgayGioDatHang;
        //string DiaChiNhan;
        //DateTime NgayGioGiaoHang;
        //double TongTien;
        //int MaTinhTrangHD;
        //string MaTaiKhoan;
        //string HinhThucThanhToan;
        //Double SoLuong;
        //String TenControl;

        // String error = "";

        //while (j <= list.Count)
        //{
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.MaHoaDon = int.Parse(Request.Form[TenControl].ToString());
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.TenKH = Request.Form[TenControl].ToString();
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.DienThoai = Request.Form[TenControl].ToString();
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.Email = Request.Form[TenControl].ToString();
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.NgayGioDatHang = DateTime.Parse(Request.Form[TenControl].ToString());
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.DiaChiNhan = Request.Form[TenControl].ToString();
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.NgayGioGiaoHang = DateTime.Parse(Request.Form[TenControl].ToString());
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.TongTien = double.Parse(Request.Form[TenControl].ToString());
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.MaTinhTrangHD =int.Parse(Request.Form[TenControl].ToString());
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.MaTaiKhoan = Request.Form[TenControl].ToString();
        //TenControl = Request.Form.AllKeys[i++].ToString();
        //dto.HinhThucThanhToan = Request.Form[TenControl].ToString();

        //SoLuong = Double.Parse(Request.Form[TenControl].ToString());

        //if (SoLuong < 1)
        //{
        //  error = error + MaHD + "_";
        //}
        //else
        //{
        //  list = new GioHangBUS().CapNhatGioHang(list, MaHD, SoLuong);
        //}

        //j++;
        //}
        //new HoaDonBUS().CapNhatHoaDon(dto);


        //Session["cms_ManageBill"] = list;

        //if (error != "")
        //{
        //    Response.Redirect("Cart.aspx?error=" + error);
        //}
        //else
        //{
        //    Response.Redirect("Cart.aspx");
        //}
    //}


  

    //protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
  //  {
       //// string currentCommand = e.CommandName;

        
        
      // int currentRowIndex = int.Parse(e.CommandArgument.ToString());

       //GridView1.Rows[0].FindControl["txtMaHD"];
       //string abc = GridView1.Rows[currentRowIndex].FindControl("txtTenKH").ToString();

      // TextBox txt = GridView1.Rows[currentRowIndex].FindControl("txtTenKH") as TextBox;
      // string abc = txt.Text.ToString();

       //string ProductID = GridView1.DataKeys[1].Value;
       ////// GridView1.DataKeys[currentRowIndex].Value.ToString();
        
       //// int index = Int32.Parse(e.CommandArgument.ToString());
       //// string ProductID = GridView1.DataKeys[index].Value.ToString();

        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{

        //    System.Data.IDataRecord rowView = (System.Data.IDataRecord)e.Row.DataItem;

        //    TableCellCollection cells = e.Row.Cells;
        //}
        // get the GridViewRow where the command is raised
        //string id = GridView1.Rows[index].Cells[2].Text;

       //GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[currentRowIndex];
       //string abc = GridView1.Rows[currentRowIndex].Cells[1].Text;
        
        
        //if (e.CommandName == "CapNhat")
        //{
        //    MarketOnlineDTO.HoaDonDTO dto = new MarketOnlineDTO.HoaDonDTO();
        //    dto.TenKH = selectedRow.Cells[1].Text;
        //    dto.DienThoai = selectedRow.Cells[2].Text;
        //    dto.Email = selectedRow.Cells[3].Text;
        //    dto.NgayGioDatHang = DateTime.Parse(selectedRow.Cells[4].Text);
        //    dto.DiaChiNhan = selectedRow.Cells[5].Text;
        //    dto.NgayGioGiaoHang = DateTime.Parse(selectedRow.Cells[6].Text);
        //    dto.TongTien = double.Parse(selectedRow.Cells[7].Text);
        //    dto.MaTinhTrangHD = int.Parse(selectedRow.Cells[8].Text);
        //    dto.MaTaiKhoan = selectedRow.Cells[9].Text;
        //    dto.HinhThucThanhToan = selectedRow.Cells[10].Text;

        //    MarketOnlineBUS.HoaDonBUS bus = new MarketOnlineBUS.HoaDonBUS();
        //    bus.CapNhatHoaDon(dto);
        //}
        
       
   // }

    //protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {

    //        //IDataRecord rowView = (IDataRecord)e.Row.DataItem;

    //        TableCellCollection cells = e.Row.Cells;
    //    }
    //}


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
        Label lbid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblMaHD");
        TextBox txtTenKH = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTenKH");
        TextBox txtDT = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDT");
        TextBox txtEmail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmail");
        TextBox txtNgayGioDatHang = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNgayGioDatHang");
        TextBox txtDiaChiNhan = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDiaChiNhan");
        TextBox txtNgayGioGiaoHang = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNgayGioGiaoHang");
        TextBox txtTongTien = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTongTien");
        TextBox txtTinhTrangHD = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTinhTrangHD");
        TextBox txtMaTK = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMaTK");
        TextBox txtHinhThucTT = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtHinhThucTT");

        HoaDonDTO dto = new HoaDonDTO();
        dto.MaHoaDon = int.Parse(lbid.Text);
        dto.TenKH = txtTenKH.Text;
        dto.DienThoai = txtDT.Text;
        dto.Email = txtEmail.Text;
        dto.NgayGioDatHang = DateTime.Parse(txtNgayGioDatHang.Text);
        dto.DiaChiNhan = txtDiaChiNhan.Text;
        dto.NgayGioGiaoHang = DateTime.Parse(txtNgayGioGiaoHang.Text);
        dto.TongTien = double.Parse(txtTongTien.Text);
        dto.MaTinhTrangHD = int.Parse(txtTinhTrangHD.Text);
        dto.MaTaiKhoan = txtMaTK.Text;
        dto.HinhThucThanhToan = txtHinhThucTT.Text;

        HoaDonBUS bus = new HoaDonBUS();

        bus.CapNhatHoaDon(dto);
        GridView1.EditIndex = -1;
        LoadDanhSachHD();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        
        LoadDanhSachHD();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadDanhSachHD();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("Insert"))
            {
                TextBox txtTenKH = (TextBox)GridView1.FooterRow.FindControl("txtNewTenKH");
                TextBox txtDT = (TextBox)GridView1.FooterRow.FindControl("txtNewDT");
                TextBox txtEmail = (TextBox)GridView1.FooterRow.FindControl("txtNewEmail");
                TextBox txtNgayGioDatHang = (TextBox)GridView1.FooterRow.FindControl("txtNewNgayGioDatHang");
                TextBox txtDiaChiNhan = (TextBox)GridView1.FooterRow.FindControl("txtNewDiaChiNhan");
                TextBox txtNgayGioGiaoHang = (TextBox)GridView1.FooterRow.FindControl("txtNewNgayGioGiaoHang");
                TextBox txtTongTien = (TextBox)GridView1.FooterRow.FindControl("txtNewTongTien");
                TextBox txtTinhTrangHD = (TextBox)GridView1.FooterRow.FindControl("txtNewTinhTrangHD");
                TextBox txtMaTK = (TextBox)GridView1.FooterRow.FindControl("txtNewMaTK");
                TextBox txtHinhThucTT = (TextBox)GridView1.FooterRow.FindControl("txtNewHinhThucThanhToan");

                HoaDonDTO dto = new HoaDonDTO();

                dto.TenKH = txtTenKH.Text;
                dto.DienThoai = txtDT.Text;
                dto.Email = txtEmail.Text;
                dto.NgayGioDatHang = DateTime.Parse(txtNgayGioDatHang.Text);
                dto.DiaChiNhan = txtDiaChiNhan.Text;
                dto.NgayGioGiaoHang = DateTime.Parse(txtNgayGioGiaoHang.Text);
                dto.TongTien = double.Parse(txtTongTien.Text);
                dto.MaTinhTrangHD = int.Parse(txtTinhTrangHD.Text);
                dto.MaTaiKhoan = txtMaTK.Text;
                dto.HinhThucThanhToan = txtHinhThucTT.Text;

                HoaDonBUS bus = new HoaDonBUS();

                bus.ThemHoaDon(dto);
                GridView1.EditIndex = -1;
                LoadDanhSachHD();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}